﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.IO;
using System.Xml.Serialization;

namespace FuelTracker.DataAccess
{
    public abstract class IsolatedStorageDao
    {
        protected IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();

        protected T Deserialize<T>(string file)
        {
            if (!isolatedStorage.FileExists(file))
                return default(T);

            using (IsolatedStorageFileStream stream = isolatedStorage.OpenFile(file, FileMode.Open))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                T data = (T)serializer.Deserialize(stream);
                return data;
            }
        }

        protected void Serialize<T>(T data, string file)
        {
            using (IsolatedStorageFileStream stream = isolatedStorage.CreateFile(file))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                serializer.Serialize(stream, data);
            }
        }

    }
}
